package com.example.thread.demo.alternate.print3;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Solution {

    private ReentrantLock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    private int i = 0;

    class Number implements Runnable {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    condition.signal();
                    if (i < 100) {
                        i++;
                        System.out.println(Thread.currentThread().getName() + "---" + i);
                    } else {
                        break;
                    }
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Solution solution = new Solution();
        Thread t1 = new Thread(solution.new Number());
        Thread t2 = new Thread(solution.new Number());
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("输入完毕");
    }
}
